package app.mvc.view.components.panels
{
	import alternativa.AlternativaGUI;
	import alternativa.gui.container.linear.HBox;
	import alternativa.gui.container.linear.LODHBox;
	import alternativa.gui.container.linear.LODVBox;
	import alternativa.gui.container.linear.RelativeHBox;
	import alternativa.gui.container.linear.VBox;
	import alternativa.gui.enum.Align;
	import alternativa.gui.layout.LayoutManager;
	import alternativa.gui.mouse.MouseManager;
	import alternativa.gui.theme.defaulttheme.container.panel.Panel;
	import alternativa.gui.theme.defaulttheme.controls.buttons.Button;
	import alternativa.gui.theme.defaulttheme.controls.text.Label;
	import alternativa.gui.theme.defaulttheme.event.PanelEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import globals.Global;
	
	import utils.UtilsGC;
	
	public class AlertPanel extends Panel
	{
		private static var _instance:AlertPanel;
		public var btnOk:Button;
		public var btnCancel:Button;
		private var contentL:Label;
		private var vBox:LODVBox;
		private var _okFunc:Function;
		public function AlertPanel()
		{
			super();
			
			init();
		}
		
		public static function getInstance():AlertPanel
		{
			if(!_instance)
				_instance = new AlertPanel();
			return _instance;
		}
		
		private function init():void
		{
			this.dragEnabled=true;
			this.closeButtonShow=true;
			
			this.title = "提示";
			btnOk = new Button();
			btnOk.label = "确定";
			btnCancel = new Button();
			btnCancel.label = "取消";
			contentL = new Label();
			contentL.align = Align.MIDDLE;
			content = vBox = new LODVBox([20,30]);
			vBox.addChild(contentL);
			
			var btnHBox:HBox = new HBox(5);
			btnOk.resize(80,25);
			btnCancel.resize(80,25);
			btnOk.freezeHeight=true;
			btnOk.freezeWidth=true;
			btnCancel.freezeHeight =true;
			btnCancel.freezeWidth=true;
			btnHBox.addChild(btnOk);
			btnHBox.addChild(btnCancel);
			vBox.addChild(btnHBox);
			
			addChild(vBox);
			
			width = 300;
			height = 120;
			
			_freezeWidth = true;
			_freezeHeight = true;
			
			btnOk.addEventListener(MouseEvent.CLICK,onBtnClick);
			btnCancel.addEventListener(MouseEvent.CLICK,onClose);
			addEventListener(PanelEvent.CLOSE,onClose);
		}
		
		private function onBtnClick(evt:MouseEvent):void
		{
			if(_okFunc!=null)
				_okFunc.call();
			onClose(null);
		}
		
		private function onClose(evt:Event):void
		{
			UtilsGC.clearObject(this);
		}
		
		public function set text($value:String):void
		{
			contentL.text = $value;
		}
		
		public function set okFunc($func:Function):void
		{
			_okFunc = $func;
		}
		
		public static function show($value:String,$okFunc:Function):void
		{
			AlertPanel.getInstance().text = $value;
			AlertPanel.getInstance().okFunc=$okFunc;
			AlertPanel.getInstance().x = (Global.stageW - AlertPanel.getInstance().width)/2;
			AlertPanel.getInstance().y = (Global.stageH - AlertPanel.getInstance().height)/2;
			LayoutManager.guiLayers[0].addChild(AlertPanel.getInstance());
		}
	}
}